<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use EasyWeChat\Foundation\Application;
use EasyWeChat\Message\Text;

class SendMsgController extends Controller
{
    private $wechat;
    public function __construct(Application $wechat)
    {
        $this->wechat = $wechat;
    }
    //
    public function index()
    {
        $now = date("Y-m-d H:i:s",time());
        //查询待发送记录
        $msgList = DB::table('msg_logs')
                ->where([
                    ['status', '=', '0'],
                    ['sendtime','<',$now]
                ])
                ->get();
        //更新状态
        DB::table('msg_logs')
            ->where([
                    ['status', '=', '0'],
                    ['sendtime','<',$now]
                ])
            ->update(['status' => 1]);

        foreach($msgList as $v){
            $text = new Text(['content' => $v->msg]);
            $this->wechat->staff->message($text)->to($v->openid)->send();
        }
    }
}
